//package com.yj.cloud.auth.service.impl;
//
//import cn.dev33.satoken.stp.StpUtil;
//import cn.dev33.satoken.stp.parameter.SaLoginParameter;
//import cn.hutool.core.collection.CollUtil;
//import com.yj.cloud.auth.domain.LoginVo;
//import com.yj.cloud.auth.form.SocialLoginBody;
//import com.yj.cloud.auth.service.IAuthStrategy;
//import com.yj.cloud.common.core.exception.ServiceException;
//import com.yj.cloud.common.core.util.JsonUtils;
//import com.yj.cloud.common.core.util.StreamUtils;
//import com.yj.cloud.common.tenant.TenantHelper;
//import com.yj.cloud.system.api.domain.vo.RemoteClientVo;
//import com.yj.cloud.system.api.domain.vo.RemoteSocialVo;
//import com.yj.cloud.system.api.feign.RemoteUserService;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Optional;
//
//@Slf4j
//@Service("social" + IAuthStrategy.BASE_NAME)
//@RequiredArgsConstructor
//public class SocialAuthStrategy implements IAuthStrategy {
//
//    private final SocialProperties socialProperties;
//
//
//    private final RemoteSocialService remoteSocialService;
//
//    private final RemoteUserService remoteUserService;
//
//    /**
//     * 登录-第三方授权登录
//     *
//     * @param body     登录信息
//     * @param client   客户端信息
//     */
//    @Override
//    public LoginVo login(String body, RemoteClientVo client) {
//        SocialLoginBody loginBody = JsonUtils.parseObject(body, SocialLoginBody.class);
//        ValidatorUtils.validate(loginBody);
//        AuthResponse<AuthUser> response = SocialUtils.loginAuth(
//                loginBody.getSource(), loginBody.getSocialCode(),
//                loginBody.getSocialState(), socialProperties);
//        if (!response.ok()) {
//            throw new ServiceException(response.getMsg());
//        }
//        AuthUser authUserData = response.getData();
//
//        List<RemoteSocialVo> list = remoteSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid());
//        if (CollUtil.isEmpty(list)) {
//            throw new ServiceException("你还没有绑定第三方账号，绑定后才可以登录！");
//        }
//        RemoteSocialVo socialVo;
//        if (TenantHelper.isEnable()) {
//            Optional<RemoteSocialVo> opt = StreamUtils.findAny(list, x -> x.getTenantId().equals(loginBody.getTenantId()));
//            if (opt.isEmpty()) {
//                throw new ServiceException("对不起，你没有权限登录当前租户！");
//            }
//            socialVo = opt.get();
//        } else {
//            socialVo = list.get(0);
//        }
//
//        LoginUser loginUser = remoteUserService.getUserInfo(socialVo.getUserId(), socialVo.getTenantId());
//        loginUser.setClientKey(client.getClientKey());
//        loginUser.setDeviceType(client.getDeviceType());
//        SaLoginParameter model = new SaLoginParameter();
//        model.setDeviceType(client.getDeviceType());
//        // 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置
//        // 例如: 后台用户30分钟过期 app用户1天过期
//        model.setTimeout(client.getTimeout());
//        model.setActiveTimeout(client.getActiveTimeout());
//        model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId());
//        // 生成token
//        LoginHelper.login(loginUser, model);
//
//        LoginVo loginVo = new LoginVo();
//        loginVo.setAccessToken(StpUtil.getTokenValue());
//        loginVo.setExpireIn(StpUtil.getTokenTimeout());
//        loginVo.setClientId(client.getClientId());
//        return loginVo;
//    }
//
//}
